/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmThongKeKhachHangTheoThang.java
 *
 * Created on May 31, 2011, 1:15:16 AM
 */

package Presentation;

import RSCall.DuongRSc;
import RSCall.PhuongRSc;
import RSCall.QuanRSc;
import RSCall.ThanhPhoRSc;
import RSCall.ThongKeThangRSc;
import Entities.THONGKETHANG;
import java.awt.HeadlessException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author VooKa
 */
public class frmThongKeKhachHangTheoThang extends javax.swing.JInternalFrame {

    /** Creates new form frmThongKeKhachHangTheoThang */
    public frmThongKeKhachHangTheoThang() {
        initComponents();
        Creation();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tbxNam = new javax.swing.JTextField();
        cbxThang = new javax.swing.JComboBox();
        btnDanhSach = new javax.swing.JButton();

        setTitle(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("titlefrmThongKeKH"));
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel1.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("month"));

        jLabel2.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel2.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("year"));

        tbxNam.setText("2011");
        tbxNam.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbxNamKeyTyped(evt);
            }
        });

        cbxThang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));

        btnDanhSach.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        btnDanhSach.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("list"));
        btnDanhSach.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDanhSachActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(cbxThang, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(tbxNam, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addComponent(btnDanhSach)
                        .addGap(230, 230, 230))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 963, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbxThang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(tbxNam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDanhSach))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 446, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbxNamKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbxNamKeyTyped
        // TODO add your handling code here:
        Utilities.Utilities.tbxKeyTyped(evt);
    }//GEN-LAST:event_tbxNamKeyTyped

    private void btnDanhSachActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDanhSachActionPerformed
        // TODO add your handling code here:
        if(tbxNam.getText().length()!=4)
            JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("namnhapkhongdung"));
        else
        {
            LoadData(cbxThang.getSelectedIndex()+1,Integer.valueOf(tbxNam.getText()));
        }
    }//GEN-LAST:event_btnDanhSachActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        this.LoadData(1, Integer.valueOf(tbxNam.getText()));
    }//GEN-LAST:event_formInternalFrameOpened


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDanhSach;
    private javax.swing.JComboBox cbxThang;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField tbxNam;
    // End of variables declaration//GEN-END:variables
    private RSCall.ThongKeThangRSc dbThongKe;
    private RSCall.ThanhPhoRSc dbThanhPho;
    private RSCall.QuanRSc dbQuan;
    private RSCall.PhuongRSc dbPhuong;
    private RSCall.DuongRSc dbDuong;
    private void Creation(){
        try {
            dbThongKe = new ThongKeThangRSc();
            dbThanhPho = new ThanhPhoRSc();
            dbQuan = new QuanRSc();
            dbPhuong = new PhuongRSc();
            dbDuong = new DuongRSc();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.toString());
            Logger.getLogger(frmThongKeKhachHangTheoThang.class.getName()).log(Level.SEVERE, null, ex);
        }        
    }
    
    private void LoadData(int thang, int nam) throws HeadlessException {
        try {
            DefaultTableModel dtm = new DefaultTableModel();
            dtm.addColumn("STT");
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("makh"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("hoten"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("address"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("soDT"));
            dtm.addColumn("Email");
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("Service"));
            ArrayList<THONGKETHANG> lst = dbThongKe.GetAllRows_Bang();
            THONGKETHANG thongKe = new THONGKETHANG();
            int n = 1;

            for (int i = 0; i < lst.size(); i++) {
                thongKe = lst.get(i);
                if (thongKe.getTuNgay().getMonth()+1 == thang && thongKe.getTuNgay().getYear()+1900 == nam) {
                    Vector _v = new Vector();
                    
                    _v.add(n++);
                    _v.add(thongKe.getMaKH());
                    _v.add(thongKe.getHoTen().trim());
                    String tenThanhPho = dbThanhPho.GetRowByID(thongKe.getDiaChiKhachHang().getMaThanhPho()).getTenThanhPho();
                    String tenQuan = dbQuan.GetRowByID(thongKe.getDiaChiKhachHang().getMaQuan()).getTenQuan();
                    String tenPhuong = dbPhuong.GetRowByID(thongKe.getDiaChiKhachHang().getMaPhuong()).getTenPhuong();
                    String tenDuong = dbDuong.GetRowByID(thongKe.getDiaChiKhachHang().getMaDuong()).getTenDuong();
                    String diaChiCT = thongKe.getDiaChiKhachHang().getDiaChiCT().trim();
                    String diaChi = diaChiCT + " " + tenDuong + " " + tenPhuong + " " + tenQuan + " " + tenThanhPho;
                    _v.add(diaChi);
                    _v.add(thongKe.getSoDienThoai().trim());
                    _v.add(thongKe.getEmail().trim());
                    _v.add(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("adboard"));
                    dtm.addRow(_v);
                }
            }
            lst = dbThongKe.GetAllRows_Bao();
            for (int i = 0; i < lst.size(); i++) {
                thongKe = lst.get(i);
                if (thongKe.getTuNgay().getMonth()+1 == thang && thongKe.getTuNgay().getYear()+1900 == nam) {
                    Vector _v = new Vector();
                    _v.add(n++);
                    _v.add(thongKe.getMaKH());
                    _v.add(thongKe.getHoTen());
                    String tenThanhPho = dbThanhPho.GetRowByID(thongKe.getDiaChiKhachHang().getMaThanhPho()).getTenThanhPho();
                    String tenQuan = dbQuan.GetRowByID(thongKe.getDiaChiKhachHang().getMaQuan()).getTenQuan();
                    String tenPhuong = dbPhuong.GetRowByID(thongKe.getDiaChiKhachHang().getMaPhuong()).getTenPhuong();
                    String tenDuong = dbDuong.GetRowByID(thongKe.getDiaChiKhachHang().getMaDuong()).getTenDuong();
                    String diaChiCT = thongKe.getDiaChiKhachHang().getDiaChiCT().trim();
                    String diaChi = diaChiCT + " " + tenDuong + " " + tenPhuong + " " + tenQuan + " " + tenThanhPho;
                    _v.add(diaChi);
                    _v.add(thongKe.getSoDienThoai());
                    _v.add(thongKe.getEmail());
                    _v.add(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("adnewspaper"));
                    dtm.addRow(_v);
                }
            }
            lst = dbThongKe.GetAllRows_ToBuom();
            for (int i = 0; i < lst.size(); i++) {
                thongKe = lst.get(i);
                if (thongKe.getTuNgay().getMonth()+1 == thang && thongKe.getTuNgay().getYear()+1900 == nam) {
                    Vector _v = new Vector();
                    _v.add(n++);
                    _v.add(thongKe.getMaKH());
                    _v.add(thongKe.getHoTen());
                    String tenThanhPho = dbThanhPho.GetRowByID(thongKe.getDiaChiKhachHang().getMaThanhPho()).getTenThanhPho();
                    String tenQuan = dbQuan.GetRowByID(thongKe.getDiaChiKhachHang().getMaQuan()).getTenQuan();
                    String tenPhuong = dbPhuong.GetRowByID(thongKe.getDiaChiKhachHang().getMaPhuong()).getTenPhuong();
                    String tenDuong = dbDuong.GetRowByID(thongKe.getDiaChiKhachHang().getMaDuong()).getTenDuong();
                    String diaChiCT = thongKe.getDiaChiKhachHang().getDiaChiCT().trim();
                    String diaChi = diaChiCT + " " + tenDuong + " " + tenPhuong + " " + tenQuan + " " + tenThanhPho;
                    _v.add(diaChi);
                    _v.add(thongKe.getSoDienThoai());
                    _v.add(thongKe.getEmail());
                    _v.add(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("handbill"));
                    dtm.addRow(_v);
                }
            }
            this.jTable1.setModel(dtm);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.toString());
        }
    }
}
